package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理模块")
public class DishController {
    @Autowired
    private DishService dishService;

    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO) {
        dishService.save(dishDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result page(Integer categoryId, String name, Integer page, Integer pageSize, Integer status) {
        DishPageQueryDTO dishPageQueryDTO = new DishPageQueryDTO();
        dishPageQueryDTO.setPage(page);
        dishPageQueryDTO.setPageSize(pageSize);
        dishPageQueryDTO.setStatus(status);
        dishPageQueryDTO.setName(name);
        dishPageQueryDTO.setCategoryId(categoryId);
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result findByCategoryId(Integer categoryId) {
        log.info("根据分类id查询菜品,id:{}",categoryId);
        List<Dish> list = dishService.findByCategoryId(categoryId);
        return Result.success(list);
    }

    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result delete(Long[] ids) {
        dishService.delete(ids);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO) {
        dishService.update(dishDTO);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询")
    public Result findById(@PathVariable Long id) {
        DishVO dish = dishService.findById(id);
        return Result.success(dish);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("禁用启用")
    public Result state(@PathVariable Integer status,Long id){
        dishService.status(status,id);
        return Result.success();
    }
}
